package com.leozhang.app;

import cn.hutool.crypto.SecureUtil;
import cn.hutool.core.util.IdUtil;

public class UserUtils {

    public static String sign(String key, String salt) {
        String signContent = key.concat("erp").concat(salt);
        // System.out.println("Method sign, signContent is: " + signContent);
        return SecureUtil.md5(signContent);
    }

    public static boolean verify(String key, String salt, String sign) {
        String signContent = sign(key, salt);
        // System.out.println("Method verify, signContent is: " + signContent);
        return sign.equals(sign(key, salt));
    }

    public static void main(String[] args) {
        String userName = "admin";

        // 用户输入原始密码，加密后传给后端
        String originPassword = "123456789aA";
        String password = SecureUtil.md5(originPassword);
        System.out.println("Orgin password: " + originPassword + ", Encrypted password: " + password);

        // 后端生成随机的salt，将加密后存入数据库中
        // String salt = IdUtil.simpleUUID();
        String salt = "e87a6b33ba824709971633f01ec888f2";
        System.out.println("Ganerate a new salt: " + salt);
        String singedString = sign(userName.trim() + password.trim(), salt);
        System.out.println(singedString);
    }

}
